//
//  BaseDefaultViewController.m
//  AirClass
//
//  Created by gupengling on 2021/7/27.
//

#import "BaseDefaultViewController.h"
#import <Masonry/Masonry.h>

@interface BaseDefaultViewController ()

@end

@implementation BaseDefaultViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
//        // 默认 phone 显示，pad 隐藏
//        self.needShowNavigationBar = kIsPhone;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [self.navigationController.navigationBar setHidden:!self.needShowNavigationBar];
    [self changeInterface];
//    if (self.navigationController.viewControllers.count > 1) {
//        [self setDefaultBackButton];
//    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
//    if (self.navigationController.viewControllers.count > 1) {
//        [self setDefaultBackButton];
//    }
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (BOOL)prefersStatusBarHidden {
    return NO;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationNone;
}

- (void)setupUI {
    [self.view addSubview:self.baseContentView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.extendedLayoutIncludesOpaqueBars = YES;
//    if (kIsPhone) {
        self.edgesForExtendedLayout = UIRectEdgeTop;
        [self.baseContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.offset(0);
        }];
        
//    } else {
//        if (self.navigationController.viewControllers.count == 1 &&
//            self.presentingViewController == nil) {
//
//            [self.baseContentView mas_makeConstraints:^(MASConstraintMaker *make) {
//                if (@available(iOS 11.0, *)) {
//                    make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft).offset(ACTabBarController.padTabBarWidth);
//                } else {
//                    make.left.offset(ACTabBarController.padTabBarWidth);
//                }
//                make.top.right.bottom.offset(0);
//            }];
//
//        } else {
//            [self.baseContentView mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.edges.offset(0);
//            }];
//        }
//    }
}

//- (void)setDefaultBackButton {
//    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kReferenceScaleWHFactor(44), kReferenceScaleWHFactor(44))];
//    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    btn.frame = CGRectMake(0, 0, kReferenceScaleWHFactor(44), kReferenceScaleWHFactor(44));
//    [btn setImage:[UIImage imageNamed:@"icon_back"] forState:(UIControlStateNormal)];
//    [btn setImageEdgeInsets:(UIEdgeInsetsMake(6, -8, 6, 8))];
//    [btn setHitButtonEdgeInsets:(UIEdgeInsetsMake(-6, -30, -6, -10))];
//    [btn addTarget:self action:@selector(goBack) forControlEvents:(UIControlEventTouchUpInside)];
//    [customView addSubview:btn];
//
//    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:customView];
//    self.navigationItem.leftBarButtonItem = item;
//}

- (void)goBack {
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark - 屏幕旋转
- (BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    if ([UIDevice zmkIsPhone]) {
        return UIInterfaceOrientationMaskPortrait;
//    } else {
//        return UIInterfaceOrientationMaskLandscape;
//    }
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//    if ([UIDevice zmkIsPhone]) {
        return UIInterfaceOrientationPortrait;
//    } else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft) {
//        return UIInterfaceOrientationLandscapeLeft;
//    } else {
//        return UIInterfaceOrientationLandscapeRight;
//    }
}

// 旋转
- (void)changeInterface {
//    if ([UIDevice zmkIsPhone]) {
        [self changeInterfaceOrientation:UIInterfaceOrientationPortrait];
//    } else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) {
//        [self changeInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
//    }
}

- (void)changeInterfaceOrientation:(UIInterfaceOrientation)orientation {
    if ([[UIApplication sharedApplication] statusBarOrientation] == orientation) return;
    
    NSNumber *unknown = [NSNumber numberWithInteger:UIInterfaceOrientationUnknown];
    [[UIDevice currentDevice] setValue:unknown forKey:@"orientation"];
    
    NSNumber *orientationNumber = [NSNumber numberWithInteger:orientation];
    [[UIDevice currentDevice] setValue:orientationNumber forKey:@"orientation"];
}

#pragma mark - lazy
- (UIView *)baseContentView {
    if (!_baseContentView) {
        _baseContentView = [[UIView alloc] init];
    }
    return _baseContentView;
}

@end
